<?php
/**
 * 文件名：brandindex.mod.php
 * 版本号：1.0
 * 最后修改时间：2006年7月18日 21:00:05
 * 作者：zhangbaofneg
 * 功能描述：品牌模块
 */
if(!defined('IN_JISHIGOU'))
{
    exit('invalid request');
}

class ModuleObject extends MasterObject
{
	
	var $ID = 0;


	
	var $ImagePath;
	
	var $sms_register = null;


	
	function ModuleObject($config)
	{
		$this->MasterObject($config);


		if(isset($this->Get['id']))
		{
			$this->ID = (int)$this->Get['id'];
		}elseif(isset($this->Post['id']))
		{
			$this->ID = (int)$this->Post['id'];
		}


		$_GET['rmod']='my';
		if(MEMBER_ID > 0) {
			$this->IsAdmin = $this->MemberHandler->HasPermission('member','admin');
		}		

		$this->Execute();
	}

	
	function Execute()
	{

		ob_start();
		$__my=$this->MemberHandler->MemberFields;
		define('_COM_NAME_',$__my['danwei']);
		define('_IS_BRAND_ADMIN_',$__my['gbquanx']);
		switch($this->Code)
		{

			case 'nav':
				$this->nav();
				
				break;
			case 'company':
				$this->company();
				
				break;
			case 'activity':
				$this->activity();
				
				break;
			case 'favorite':
				$this->favorite();
				
				break;
			case 'detail':
				$this->detail();
				
				break;	
			case 'comment':
				$this->comment();
			break;	
			default:
				$this->Main();
				
				break;
		}
		$Contents=ob_get_clean();
		$this->ShowBody($Contents);
	}

	function Main()
	{
	
	    //print_r($_COOKIE);
		$sql = "select * from `".TABLE_PREFIX."slide` order by sord asc";
		$query = $this->DatabaseHandler->Query($sql);
		$brandslide = array();
		while ($row = $query->GetRow()){
		$brandslide[] = $row;
		}
		unset($row);
		$query = $this->DatabaseHandler->Query("select id,title from ".TABLE_PREFIX."event order by id desc limit 5");
		while ($row = $query->GetRow()){
		$huodongs[] = $row;
		}
		unset($row);
		$sql = "SELECT a.id,a.pic, a.`tobacco_name` , b.brandseri
FROM `jy_tobacco` AS a
LEFT JOIN `jy_brandclass` AS b ON a.seriesID = b.id
WHERE b.brandseri <> ''
ORDER BY a.`recommendsum` DESC 
LIMIT 9 ";
		$query = $this->DatabaseHandler->Query($sql);
		$tobaccolist = array();
		while ($row = $query->GetRow()){
		$tobaccolist[] = $row;
		}
		unset($row);
		$query = $this->DatabaseHandler->Query("SELECT id, `tobacco_name` , `pic` FROM `jy_tobacco` ORDER BY `id` DESC LIMIT 6 ");
		while ($row = $query->GetRow()){
		$newtobacco[] = $row;
		}
		unset($row);
		
		$sql = "select * from `jy_guanbo`  ";
	    $query = $this->DatabaseHandler->Query($sql);
	    while ($row = $query->GetRow()){
		  $guanbos[] = $row;
	    }
		unset($row);
		
		
		include(ROOT_PATH . './templates/app/brand_index.php');
		
	}

       /*  时间戳判断
	    $day = 7;
		$time = $day * 86400;
		$dateline = time() - $time;
		WHERE dateline>=$dateline
	   */
	
	function nav(){
		//extract($this->Get);
		if(isset($this->Get['recommendid']) && empty($this->Get['uid'])){
		 echo 're';die();
		}
		if(isset($this->Get['id']) && empty($this->Get['uid'])){
		 echo 're';die();
		}
		if(isset($this->Get['uid']) && isset($this->Get['id']) && !empty($this->Get['uid'])){
		$uid = intval($this->Get['uid']);
		$id = intval($this->Get['id']);
		$sql = "select * from `".TABLE_PREFIX."recommend` where `userid`= $uid and `tobaccoid`=$id";
	 	$query = $this->DatabaseHandler->Query($sql);
		if($row = $query->GetRow()){
		 echo '您已推荐过';die();
		}else{
		$sql = "insert into `".TABLE_PREFIX."recommend` values ('','$uid','$id')";
	 	$query = $this->DatabaseHandler->Query($sql);
		 echo '推荐成功';die();
		}
		}
		if(isset($this->Get['uid']) && isset($this->Get['recommendid']) && !empty($this->Get['uid'])){
		$uid = intval($this->Get['uid']);
		$id = intval($this->Get['recommendid']);
		$sql = "select * from `".TABLE_PREFIX."brandfavorite` where `userid`= $uid and `tobaccoid`=$id";
	 	$query = $this->DatabaseHandler->Query($sql);
		if($row = $query->GetRow()){
		 echo '您已收藏过';die();
		}else{
		$sql = "insert into `".TABLE_PREFIX."brandfavorite` values ('','$uid','$id')";
	 	$query = $this->DatabaseHandler->Query($sql);
		 echo '收藏成功';die();
		}
		}
		$brandseri='';
		$where='';
		$order='';
        $sql = "select * from `".TABLE_PREFIX."brandclass` ";
	 	$query = $this->DatabaseHandler->Query($sql);
		 $brandtype_list=array();
		while($row = $query->GetRow())
		{	
			$brandtype_list[$row['company']][]=array($row['id'],$row['brandseri'],$row['tobacconame']);
			$brandseri .='<a href="index.php?mod=brandindex&code=nav&serid='.$row['id'].'">'.$row['brandseri'].'</a>';
		}
		$sql = "SELECT `tar` FROM  `jy_tobacco` WHERE  `tobacco_name` <>  '' and `tar`<>'' GROUP BY  `tar`  ";
	 	$query = $this->DatabaseHandler->Query($sql);
		$tar_list='';
		while($row = $query->GetRow())
		{	
		   if($this->Get['tar']==$row['tar']){
		   $tar_list .='<a href="index.php?mod=brandindex&code=nav&tar='.$row['tar'].'" class="this">'.$row['tar'].'</a>';
		   }else{
			$tar_list .='<a href="index.php?mod=brandindex&code=nav&tar='.$row['tar'].'">'.$row['tar'].'</a>';
			}
		}
		if(isset($this->Get['serid']) && !empty($this->Get['serid'])){
		$serid = intval($this->Get['serid']);
		$where .=' `seriesID`= '.$serid.' and';
		}elseif(isset($this->Get['year']) && !empty($this->Get['year'])){
		$year = intval($this->Get['year']);
		$where .='   LEFT(`markettime`,4)= '.$year.' and';
		}elseif(isset($this->Get['tar']) && !empty($this->Get['tar'])){
		$tar = $this->Get['tar'];
		$where .=' `tar`= '.$tar.' and';
        }
		$where = !empty($where)? ' where '.substr($where,0,-3):'';
	   if(isset($this->Get['price']) && !empty($this->Get['price'])){
	    $price = $this->Get['price'];
		$order =' order by `w_price` '.$price;
	   }elseif(isset($this->Get['renqi']) && !empty($this->Get['renqi'])){
	    $renqi = $this->Get['renqi'];
		$order =' order by `recommendsum` '.$renqi;
	   }
	   
	   $per_page_num = min(500,max((int) $_GET['per_page_num'],(int) $_GET['pn'],10));
	   $query_link = 'index.php?mod=brandindex&code=nav';
		$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."tobacco` $where ";	
		$query = $this->DatabaseHandler->Query($sql);
		extract($query->GetRow());
		$page_arr = pagebrand($total_record,$per_page_num,$query_link,array('return'=>'array',));
	   $sql = "select * from `".TABLE_PREFIX."tobacco`  $where  $order {$page_arr['limit']} ";
	   $query = $this->DatabaseHandler->Query($sql);
	   while ($row = $query->GetRow()){
		$navtobacco[] = $row;
		}	
	   include(ROOT_PATH . './templates/app/brand_nav.php');	
	}

   function company(){
	  $sql = "select * from `jy_guanbo`  ";
	   $query = $this->DatabaseHandler->Query($sql);
	   while ($row = $query->GetRow()){
		$guanbos[] = $row;
	   }	

	include(ROOT_PATH . './templates/app/brand_company.php');		
	}
	
	
	function activity(){
	$userid = $_COOKIE['MEMBER_ID'];
	$query = $this->DatabaseHandler->Query("select id,title,image,FROM_UNIXTIME(`fromt`,'%Y-%m-%d %H:%m:%s') as`fromt`,FROM_UNIXTIME(`tot`,'%Y-%m-%d %H:%m:%s') as`tot`,address,postman,content  from ".TABLE_PREFIX."event order by id desc limit 3");
		while ($row = $query->GetRow()){
		$huodonghdp[] = $row;
		}
     $query = $this->DatabaseHandler->Query("SELECT `id` , `title` , `image` , `content` FROM `jy_event` 
WHERE id NOT IN (SELECT id FROM jy_event_member WHERE fid =$userid)");
		while ($row = $query->GetRow()){
		$nohuodongs[] = $row;
		}
	$query = $this->DatabaseHandler->Query("SELECT a.id, a.title, b.image, b.content
FROM jy_event_member AS a
LEFT JOIN jy_event AS b ON a.id = b.id
WHERE a.fid =$userid");
		while ($row = $query->GetRow()){
		$myhuodongs[] = $row;
		}	
	include(ROOT_PATH . './templates/app/brand_activity.php');			
	}
	
	function favorite(){
		if(isset($this->Get['favid']) && !empty($this->Get['favid'])){
			$sql = "delete from " . TABLE_PREFIX . "brandfavorite where id=".$this->Get['favid'];
			$query = $this->DatabaseHandler->Query($sql);
			if( $query ){
				echo "收藏已删除！";
			}else{
				echo "删除失败！";
			};
		}else{
			
			$sql = "SELECT * FROM " . TABLE_PREFIX . "brandfavorite where userid = ".$_COOKIE['MEMBER_ID'];
			$query = $this->DatabaseHandler->Query($sql);
			$myfavorite = array();
			while ($value = $query->GetRow()){
		
				$tInfo = $this->DatabaseHandler->Query("SELECT * FROM " . TABLE_PREFIX . "tobacco where id=".$value['tobaccoid']);
				$tInfo = $tInfo->GetRow();
				$tInfo['favid'] = $value['id'];
				$tInfo['uid'] = $value['userid'];
				$sqla = "SELECT a.`userid` , a.`tobaccoid` , a.`content` , a.`posttime` , b.`username` , b.`nickname` , b.`face` 
					FROM `jy_liuyan` AS a
					LEFT JOIN `jy_members` AS b ON a.`userid` = b.`uid` 
					WHERE a.`tobaccoid` ={$value['tobaccoid']} order by a.id desc limit 1";
				$liuyan = $this->DatabaseHandler->Query($sqla);
				$liuyan = $liuyan->GetRow();
				$tInfo['liuyan'] = $liuyan;
				$myfav[] = $tInfo;
			}
			
			include(ROOT_PATH . './templates/app/brand_favorites.php');	
		}
	}

	function detail(){
	    $ids = (int) $this->Get['ids'];
		$sql = "select * from `".TABLE_PREFIX."tobacco` where `id`=$ids ";
		$query = $this->DatabaseHandler->Query($sql);
		$todetail=$query->GetRow();
		$serid = $todetail['seriesID'];
		$sql = "select * from `".TABLE_PREFIX."brandclass` where `id`=$serid ";
	 	$query = $this->DatabaseHandler->Query($sql);
		$tobnav = $query->GetRow();
		
		$sql = "select * from `".TABLE_PREFIX."morepic` where `tobaccoid`=$ids ";
		$query = $this->DatabaseHandler->Query($sql);
		$morepic = $query->GetRow();
		$morepics = split('[|]',$morepic['tobpic']);
		$sql = "select * from `".TABLE_PREFIX."brandclass` ";
	 	$query = $this->DatabaseHandler->Query($sql);
		 $brandtype_list=array();
		while($row = $query->GetRow())
		{	
			$brandtype_list[$row['company']][]=array($row['id'],$row['brandseri']);
			
		}
     $sql = "SELECT a.`userid` , a.`tobaccoid` , a.`content` , a.`posttime` , b.`username` , b.`face` 
FROM `jy_liuyan` AS a
LEFT JOIN `jy_members` AS b ON a.`userid` = b.`uid` 
WHERE a.`tobaccoid` =$ids ";
	 $query = $this->DatabaseHandler->Query($sql);
	 while($row = $query->GetRow())
			{	
			$liuyan_list[]=$row;
			}
		 $sql = "select * from `".TABLE_PREFIX."pingjia` where `tobaccoid`=$ids ";
		 $query = $this->DatabaseHandler->Query($sql);
		  $k= $m =$n =$bz= $kw=0;
		  while($row = $query->GetRow()){
		  $k++;
		  if($row['baozhuang']!=0){
		  $bz+=$row['baozhuang'];
		  $m++;
		  }
		  if($row['kouwei']!=0){
		  $kw+=$row['kouwei'];
		  $n++;
		  }
	}
	  $perbz = round($bz/$m,1);
	  $perkw = round($kw/$n,1);
	  $perzh = round(($bz+$kw)/($m+$n),1);
	include(ROOT_PATH . './templates/app/brand_product.php');			
	}
	
	
	function comment(){
	 if(!isset($_COOKIE['MEMBER_ID']) || empty($_COOKIE['MEMBER_ID'])){
	  echo 1;die();
	 }
	if(isset($this->Post['userid']) && $this->Post['userid']==$_COOKIE['MEMBER_ID'])
	 if(empty($this->Post['content'])){
	  die();
	 }
	 $posttime = date('Y-m-d H:i:s');
	 $userid = $this->Post['userid'];
	 $tobid = $this->Post['tobid'];
	 $content = $this->Post['content'];
	 $html ='<dl>';
	 $html .='<dt><a href="###"><img src="templates/app/images/user03.jpg" width="40" height="40" /><br />'.$_COOKIE['MEMBER_NAME'].'</a></dt>';
	 $html .='<dd class="text">'.$content.'</dd>';
	 $html .='<dd class="date">'.$posttime.'</dd>';
	 $html .='</dl>	<div class="float_both"></div>';
	 $sql = "insert into `".TABLE_PREFIX."liuyan` values ('',$userid,$tobid,'$content','$posttime')";
	 if($query = $this->DatabaseHandler->Query($sql)){
		 echo $html;
		}
	if(!empty($this->Post['star1']) || !empty($this->Post['star2'])){
	$kouwei =!empty($this->Post['star1'])?intval($this->Post['star1']):0;
	$baozhuang = !empty($this->Post['star2'])?intval($this->Post['star2']):0;
	$sql = "insert into `".TABLE_PREFIX."pingjia` values ('',$userid,$tobid,'$kouwei','$baozhuang')";
	$query = $this->DatabaseHandler->Query($sql);
	}	
	}
	
	
}

?>
